﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using ORS.BLL;

namespace ORS.Web
{
    public partial class Login : System.Web.UI.Page
    {
          
        protected void Page_Load(object sender, EventArgs e)
        {
            Table_Err1.Visible = false;
            Table_Err2.Visible = false;
        }

        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        protected void TextPassword_TextChanged(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string sAdminID = this.tbxuserID.Text;
            string sAdminPWD = this.TextPassword.Text;
            string stablename = "tb_admin";
            int getnum=0;
            processopr ivk_processopr = new processopr();
            getnum = ivk_processopr.IsUserExisting(stablename, sAdminID, sAdminPWD);
            switch (getnum)
                {
                   case 1:
                        Table_Err1.Visible=true;break;
                   case 2:
                        Table_Err2.Visible=true;break;
                   case 3:
                        Session["Admim_IDS"]=sAdminID;Response.Redirect("Index_Adm.aspx");break;
                   default:
                        break;
                }

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string sAdminID = this.tbxuserID.Text;
            string sAdminPWD = this.TextPassword.Text;
            string stablename = "tb_userlist";
            int getnum = 0;
            processopr ivk_processopr = new processopr();
            getnum = ivk_processopr.IsUserExisting(stablename, sAdminID, sAdminPWD);
            switch (getnum)
            {
                case 1:
                    Table_Err1.Visible = true; break;
                case 2:
                    Table_Err2.Visible = true; break;
                case 3:
                    pageChange(); break;
                default:
                    break;
            }
        }

        void pageChange()
        {
            string sAdminID = this.tbxuserID.Text;
            BLL.roleselect roleSelect = new BLL.roleselect();
            string rolename = roleSelect.SelectRole(sAdminID).Trim();
            if (rolename =="作者")
            {
                Session["userid"] = sAdminID;
                Response.Redirect("User_Mainpage.aspx");
            }
            if (rolename ==  "审稿人")
            {
                Session["reviewerid"] = sAdminID;
                Response.Redirect("Review_Mainpage.aspx");
            }
        }

    }
}

// To BLL:
// public class processopr 
// {
//   string sqlstr1;
//   string sqlstr2;
//   mssqlopr ivk_mssqlopr;
//   int number;
//
//   public processopr()
//       {
//        sqlstr1="";
//        sqlstr2="";
//        number=0;
//        }
//
//   public int IsUserExisting(string tb_name,string user_name,string user_password)
//       {
//        sqlstr1 = "SELECT * FROM {0} WHERE userid='{1}'";
//        sqlstr2 = "SELECT * FROM {0} WHERE userid='{1}'AND password='{2}'";
//        ivk_mssqlopr = new mssqlopr();
//        if (!ivk_mssqlopr.find(sqlstr1))
//            {
//               number=1;
//               return number;
//            } 
//            elseif (!ivk_mssqlopr.find(sqlstr2))
//                   {
//                      number=2;
//                      return number;
//                    }
//                    else 
//                        {
//                          number=3;
//                          return mumber;
//                        }
//       }
// }


// To DAL:
// public class mssqlopr
// {
//      private string scfgcon;
//      private System.Data.SqlClient.SqlConnection Conn;
//      private System.Data.SqlClient.SqlCommand cmd;
//      private System.Data.SqlClient.SqlDataReader reader; 



//      public mssqlopr()
//        { 
//          scfgcon = System.Configuration.ConfigurationManager.ConnectionStrings["DB_ConnectionString"].ConnectionString;
//          Conn = new SqlConnection(scfgcon);
//         }
//
//      public bool find_User(string sqlcmdtext)
//         {  
//           Conn.Open();
//           cmd = new SqlCommand(sqlcmtext,Conn);
//           reader = cmd.ExecuteReader();
//           if (reader.Read())
//              {
//                  conn.Close();
//                  return true;     
//               }
//           else
//              {
//                  conn.Close();
//                  returen false;
//               }
//          }
//
// }